﻿using UnityEngine;

[CreateAssetMenu]
public class MonsterFactory : ScriptableObject
{
    [SerializeField]
    private Monster[] prefabs = default;

    //出生点
    [SerializeField]
    Vector3[] spawnPoints = default;

    //散布范围
    [SerializeField, Range(0, 2)]
    private float spreadRange ;

    private MonsterConfig config = new MonsterConfig();


    public Monster Get(EnumMonster type)
    {
        //优先从缓存池获取
        Monster monster = Mgr.monsterMgr.monsterList.GetNewMonster(type);
        if (monster == null)
            monster = Instantiate(prefabs[(int)type]);
        else
            monster.gameObject.SetActive(true);
        monster.SpawnOn( GetRandomSpawnPoint());
        monster.monsterType = type;
        //随机生成唯一ID
        int id;
        do
        {
            id = Random.Range(1000, 9999);
        }
        while(Mgr.monsterMgr.IsContainMonster(id));

        monster.MonsterId = id;

        //随机怪物属性
        PlayerInfo info = new PlayerInfo
        {
            //Health = (int)config.health.RandomValueInRange,
            //Attack = (int)config.attack.RandomValueInRange,
            //Defense = (int)config.defense.RandomValueInRange,
            //Speed = (int)config.speed.RandomValueInRange,
            Health = config.health,
            Attack = config.attack,
            Defense = config.defense,
            Speed = config.speed,
            Name = monster.MonsterId.ToString()
            
        };
        monster.SetMonsterInfo(info);
        return monster;
    }

    //随机出生点
    private Vector3 GetRandomSpawnPoint()
    {
        if(spawnPoints.Length == 0)
        {
            Debug.Log("no Spawn Point!");
            return Vector3.zero;
        }

        Vector3 pos = spawnPoints[Random.Range(0, spawnPoints.Length)];
        pos.x += Random.Range(-spreadRange, spreadRange);
        pos.z += Random.Range(-spreadRange, spreadRange);
        return pos;
    }

    [System.Serializable]
    class MonsterConfig
    {//怪物配置

        public int health = 100;

        public int attack = 20;

        public int defense = 5;

        public int speed = 5;
        
        ////生命值
        ////[AttributeRange(100f, 200f)]
        //public FloatRange health = new FloatRange(100f, 200f);

        ////[AttributeRange(20f, 50f)]
        //public FloatRange attack = new FloatRange(20f, 50f);

        ////[AttributeRange(0f, 50f)]
        //public FloatRange defense = new FloatRange(4f, 10f);

        ////[AttributeRange(0f, 50f)]
        //public FloatRange speed = new FloatRange(4f, 7f);

    }
}